<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const roleDetail = ref(null)

// Mock 数据
const mockRoleDetails = {
  1: {
    id: 1,
    code: 'ROLE001',
    name: '人事专员',
    description: '负责员工档案管理、登记、查询和删除',
    status: '启用',
    createTime: '2024-01-15 10:30:00',
    updateTime: '2024-01-20 14:20:00',
  },
  2: {
    id: 2,
    code: 'ROLE002',
    name: '复核专员',
    description: '负责档案和薪酬的复核工作',
    status: '启用',
    createTime: '2024-01-15 10:35:00',
    updateTime: '2024-01-18 09:15:00',
  },
  3: {
    id: 3,
    code: 'ROLE003',
    name: '部门经理',
    description: '可以查询本部门员工档案和薪酬信息',
    status: '启用',
    createTime: '2024-01-15 10:40:00',
    updateTime: '2024-01-15 10:40:00',
  },
  4: {
    id: 4,
    code: 'ROLE004',
    name: '普通员工',
    description: '可以查询自己的档案和薪酬信息',
    status: '启用',
    createTime: '2024-01-15 10:45:00',
    updateTime: '2024-01-15 10:45:00',
  },
  5: {
    id: 5,
    code: 'ROLE005',
    name: '系统专员',
    description: '负责系统设置，包括机构关系和职位设置',
    status: '启用',
    createTime: '2024-01-16 11:00:00',
    updateTime: '2024-01-16 11:00:00',
  },
  6: {
    id: 6,
    code: 'ROLE006',
    name: '薪酬专员',
    description: '负责薪酬标准登记、变更和发放登记',
    status: '启用',
    createTime: '2024-01-16 11:05:00',
    updateTime: '2024-01-16 11:05:00',
  },
  7: {
    id: 7,
    code: 'ROLE007',
    name: '权限管理员',
    description: '负责角色权限分配和管理',
    status: '启用',
    createTime: '2024-01-17 09:00:00',
    updateTime: '2024-01-17 09:00:00',
  },
}

// 获取角色详情（使用 mock 数据）
const fetchRoleDetail = async () => {
  loading.value = true
  try {
    // 模拟 API 延迟
    await new Promise((resolve) => setTimeout(resolve, 300))

    const roleId = Number(route.params.id)
    const detail = mockRoleDetails[roleId]

    if (detail) {
      roleDetail.value = detail
    } else {
      // 如果找不到，使用默认数据
      roleDetail.value = {
        id: roleId,
        code: `ROLE${String(roleId).padStart(3, '0')}`,
        name: '未知角色',
        description: '角色信息不存在',
        status: '停用',
        createTime: '-',
        updateTime: '-',
      }
    }
  } catch (error) {
    console.error('获取角色详情失败:', error)
    roleDetail.value = null
  } finally {
    loading.value = false
  }
}

const handleBack = () => {
  router.push('/role')
}

onMounted(() => {
  fetchRoleDetail()
})
</script>

<template>
  <div class="page-container">
    <div class="page-header">
      <div class="header-left">
        <h1 class="page-title">角色详情</h1>
        <p class="page-desc">查看角色的详细信息</p>
      </div>
    </div>

    <div class="page-content">
      <a-spin :spinning="loading">
        <div v-if="roleDetail" class="detail-wrapper">
          <div class="detail-section">
            <div class="detail-item">
              <span class="detail-label">角色编码：</span>
              <span class="detail-value">{{ roleDetail.code }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">角色名称：</span>
              <span class="detail-value">{{ roleDetail.name }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">角色描述：</span>
              <span class="detail-value">{{ roleDetail.description }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">状态：</span>
              <a-tag :color="roleDetail.status === '启用' ? 'green' : 'red'">
                {{ roleDetail.status }}
              </a-tag>
            </div>
            <div class="detail-item">
              <span class="detail-label">创建时间：</span>
              <span class="detail-value">{{ roleDetail.createTime }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">更新时间：</span>
              <span class="detail-value">{{ roleDetail.updateTime }}</span>
            </div>
          </div>
        </div>
        <div v-else class="empty-state">
          <p>未找到角色信息</p>
        </div>
      </a-spin>

      <div class="page-footer">
        <a-button type="default" @click="handleBack">
          <template #icon>←</template>
          返回列表
        </a-button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.page-container {
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(20px) saturate(180%);
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.04);
}

.page-header {
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.header-left {
  display: flex;
  flex-direction: column;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #1d1d1f;
  margin: 0 0 8px 0;
  letter-spacing: -0.5px;
}

.page-desc {
  font-size: 14px;
  color: #86868b;
  margin: 0;
}

.page-content {
  margin-top: 24px;
}

.page-footer {
  margin-top: 24px;
  padding-top: 24px;
  border-top: 1px solid rgba(0, 0, 0, 0.06);
  display: flex;
  justify-content: flex-end;
}

.detail-wrapper {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.02);
}

.detail-section {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.detail-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.detail-item:last-child {
  border-bottom: none;
}

.detail-label {
  font-weight: 500;
  color: #86868b;
  min-width: 120px;
  font-size: 14px;
}

.detail-value {
  color: #1d1d1f;
  font-size: 14px;
  flex: 1;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #86868b;
}
</style>
